package cn.com.infcn.spider.web.collector;

import org.jsoup.Jsoup;
import org.nlpcn.commons.lang.util.StringUtil;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.Param;

import com.alibaba.fastjson.JSONObject;

import cn.com.infcn.spider.util.HttpUtils;
import cn.com.infcn.spider.util.URLUtil;

@IocBean
public class DebugAction {

	@At("/debug/gathor")
	@Ok("json")
	public Object gathor(@Param("url") String url,@Param("ajax") String ajax) {
		JSONObject result = new JSONObject();
		
		String html = null ;
		if(StringUtil.isBlank(ajax)){
			html = HttpUtils.getPage(url, 20000).getHtml() ;
		}else{
			html = HttpUtils.getPageByJs(url).getHtml() ;
		}
		
		result.put("html", html);
		return result;
	}

	@At("/debug/extract")
	@Ok("json")
	public Object extract(@Param("html") String html, @Param("select") String select) {
		String text = URLUtil.extracting(Jsoup.parse(html), select);
		JSONObject result = new JSONObject();
		result.put("result", text);
		result.put("code", "_doc.setValue('result','" + select + "')");
		return result;
	}

}
